//
//  GQHRouteFreightsController.m
//  Xanthium
//
//  Created by GuanQinghao on 08/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHRouteFreightsView.h"

#pragma mark Controller
#import "GQHRouteFreightsController.h"
#import "GQHFreightInfoController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHRouteFreightsController () <GQHRouteFreightsViewDelegate> {
    
#pragma mark Data
    /// 请求参数
    __block NSMutableDictionary *parameters;
    /// 返回模型数组
    __block NSMutableArray<GQHFreightModel *> *freightModelArray;
#pragma mark View
    /// 自定义根视图
    GQHRouteFreightsView *rootView;
}

@end


@implementation GQHRouteFreightsController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHRouteFreightsView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    
    if (_qh_routeModel) {
        DLog(@"路线货源");
        
        [parameters setObject:USER_GET_ID forKey:@"token"];
        [parameters setObject:_qh_routeModel.qh_departureID forKey:@"origin"];
        [parameters setObject:_qh_routeModel.qh_destinationID forKey:@"destination"];
        
        [self fetchRouteFreightsWithParameters:parameters];
    } else {
        DLog(@"回程货源");
        
        [parameters setObject:USER_GET_ID forKey:@"driveId"];
        
        [self fetchReturenRouteFreightsWithParameters:parameters];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    if (_qh_routeModel) {
        DLog(@"路线货源");
        
        self.qh_titleLabel.text = [NSString stringWithFormat:@"%@-%@",_qh_routeModel.qh_departure,_qh_routeModel.qh_destination];
    } else {
        DLog(@"回程货源");
        
        self.qh_titleLabel.text = @"回程货源";
    }
    
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [rootView.qh_tableView.mj_header endRefreshing];
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    GQHFreightInfoController *freightInfoController = [[GQHFreightInfoController alloc] init];
    freightInfoController.qh_freightModel = freightModelArray[indexPath.row];
    freightInfoController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:freightInfoController animated:YES];
}

- (void)qh_dialMobile:(UIButton *)sender {
    DLog(@"联系货主");
    
    GQHFreightModel *freightModel = freightModelArray[sender.tag];
    [self qh_dialWithMobile:freightModel.qh_posterMobile];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
/// 路线货源
- (void)fetchRouteFreightsWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHFreightModel qh_fetchFreightsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHFreightModel *> *freights) {
            
            if (status) {
                
                freightModelArray = [NSMutableArray arrayWithArray:freights];
                rootView.qh_freightModelArray = freightModelArray;
                [rootView.qh_tableView reloadData];
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHFreightModel qh_fetchFreightsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHFreightModel *> *freights) {
            
            if (status) {
                
                if (freights && freights.count > 0) {
                    
                    [freightModelArray addObjectsFromArray:freights];
                    rootView.qh_freightModelArray = freightModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

/// 回程货源
- (void)fetchReturenRouteFreightsWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHFreightModel qh_fetchFreightsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHFreightModel *> *freights) {
            
            if (status) {
                
                freightModelArray = [NSMutableArray arrayWithArray:freights];
                rootView.qh_freightModelArray = freightModelArray;
                [rootView.qh_tableView reloadData];
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHFreightModel qh_fetchFreightsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHFreightModel *> *freights) {
            
            if (status) {
                
                if (freights && freights.count > 0) {
                    
                    [freightModelArray addObjectsFromArray:freights];
                    rootView.qh_freightModelArray = freightModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
